/* ***************************
 Website:  http://wch.cn
 Email:    tech@wch.cn
 Author:   W.ch 2008.7
* ****************************/
#include <BSP_Sleep.h>
#include "CH455I2C.H"	// 修改该文件以适应硬件环境/单片机型号等

#define USE_CH455_KEY	1

uint8_t ch455_key;//CH455的按键键值

const uint8_t BCD_decode_tab[0x10] = { 0X3F, 0X06, 0X5B, 0X4F, 0X66, 0X6D, 0X7D, 0X07, 0X7F, 0X6F, 
									 0X77, 0X7C, 0X58, 0X5E, 0X79, 0X71 };//BCD

void mDelaymS( uint8_t ms )// 延时毫秒
{
	BSP_Sleep_Ms(ms);
}

void init_CH455( void ) //初始化CH455
{
}

#if 0
void int1( void ) interrupt 2 using 1 // INT0中断服务程序
{
	ch455_key = CH455_Read();
}
#endif 

#if 0
void CH455_Test( void )//主程序
{
	uint8_t i;
    uint8_t showH[2],showL[2];

	CH455_Init();//初始化

	CH455_Write( CH455_7SEG_ON );// 开启显示和键盘，7段显示方式
	mDelaymS( 50 );

	//4位数码管全部显示0
	for(i = 0; i < 10; i++){
		CH455_Write( CH455_DIG3 | 0x7F );
		CH455_Write( CH455_DIG2 | 0x7F );
		mDelaymS(100);
		CH455_Write( CH455_DIG3 | 0 );
		CH455_Write( CH455_DIG2 | 0 );
		mDelaymS(100);
	}
    
    
	while(1)
	{
	#ifdef USE_CH455_KEY

		if(CH455_Check()){
			ch455_key = CH455_Read();
			if(ch455_key > 0x40){
				CH455_Write( CH455_DIG3 | 0x7F );
				CH455_Write( CH455_DIG2 | 0x7F );
				mDelaymS(100);
				CH455_Write( CH455_DIG3 | 0 );
				CH455_Write( CH455_DIG2 | 0 );
			}
		}


    #endif
	}
												
}
#endif 


void CH455_Test( void )//主程序
{
	uint8_t i;

	CH455_Init();//初始化

	CH455_Write( CH455_7SEG_ON );// 开启显示和键盘，7段显示方式
//		BSP_Keyboard_Init();
	for(i = 0; i < 8; i++){
		
		CH455_Write( CH455_SYSON | i<<4 | CH455_BIT_7SEG); // 1-7级亮度显示
		
		CH455_Write( CH455_DIG3 | 0x7F );
		CH455_Write( CH455_DIG2 | 0x7F );
		mDelaymS(100);
		CH455_Write( CH455_DIG3 | 0 );
		CH455_Write( CH455_DIG2 | 0 );
		mDelaymS(100);
	}
	CH455_Write( CH455_DIG3 | 0x7F );
	CH455_Write( CH455_DIG2 | 0x7F );
	CH455_Write( CH455_SYSON_8 );	// 8级亮度显示
	while(1){
		mDelaymS( 50 );
		if(BSP_Keyboard_Check()){
			int key = BSP_Keyboard_Scan();
			if(key >= 0){
				uint32_t map = BSP_Keyboard_ValToMap(key);
				BSP_KeyboardBacklight_Set(map, 0);
			}
		}
		else{
			int key = BSP_Keyboard_Scan();
			if(key < 0){
				BSP_KeyboardBacklight_SetAll(1);
			}
		}
	}
}


